import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

public class WithdrawService {
    private UserService userService;
    private WithdrawRecordRepository withdrawRecordRepository;
    
    public WithdrawService() {
        this.userService = new UserService();
        this.withdrawRecordRepository = new WithdrawRecordRepository();
    }
    
    public WithdrawRecord createWithdrawRecord(Long userId, BigDecimal amount, String withdrawType, String accountInfo) {
        // 验证参数
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("提现金额必须大于0");
        }
        
        if (!isValidWithdrawType(withdrawType)) {
            throw new IllegalArgumentException("不支持的提现类型");
        }
        
        // 检查用户余额
        User user = userService.getUserById(userId);
        if (user.getBalance().compareTo(amount) < 0) {
            throw new InsufficientBalanceException("账户余额不足");
        }
        
        // 创建提现记录
        WithdrawRecord record = new WithdrawRecord();
        record.setWithdrawNo(generateWithdrawNo());
        record.setUserId(userId);
        record.setAmount(amount);
        record.setWithdrawType(withdrawType);
        record.setStatus("PENDING");
        record.setCreateTime(LocalDateTime.now());
        
        // 根据提现类型设置账户信息
        switch (withdrawType) {
            case "WECHAT":
                record.setWechatOpenId(accountInfo);
                break;
            case "ALIPAY":
                record.setAlipayAccount(accountInfo);
                break;
            case "BANK_CARD":
                record.setBankCardNo(accountInfo);
                break;
        }
        
        // 保存记录
        return withdrawRecordRepository.save(record);
    }
    
    public boolean approveWithdraw(String withdrawNo) {
        WithdrawRecord record = withdrawRecordRepository.findByWithdrawNo(withdrawNo);
        if (record == null || !"PENDING".equals(record.getStatus())) {
            return false;
        }
        
        // 扣减用户余额
        userService.deductBalance(record.getUserId(), record.getAmount());
        
        // 更新提现记录状态
        record.setStatus("APPROVED");
        record.setProcessTime(LocalDateTime.now());
        withdrawRecordRepository.save(record);
        
        return true;
    }
    
    public boolean rejectWithdraw(String withdrawNo, String remark) {
        WithdrawRecord record = withdrawRecordRepository.findByWithdrawNo(withdrawNo);
        if (record == null || !"PENDING".equals(record.getStatus())) {
            return false;
        }
        
        // 更新提现记录状态
        record.setStatus("REJECTED");
        record.setRemark(remark);
        record.setProcessTime(LocalDateTime.now());
        withdrawRecordRepository.save(record);
        
        return true;
    }
    
    public WithdrawRecord getRecordByWithdrawNo(String withdrawNo) {
        return withdrawRecordRepository.findByWithdrawNo(withdrawNo);
    }
    
    private String generateWithdrawNo() {
        return "WD" + UUID.randomUUID().toString().replace("-", "").substring(0, 28);
    }
    
    private boolean isValidWithdrawType(String withdrawType) {
        return "WECHAT".equals(withdrawType) || 
               "ALIPAY".equals(withdrawType) || 
               "BANK_CARD".equals(withdrawType);
    }
}